package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.model.dto.CMReportDTO;
import com.ttg.model.mapper.CostManageIndustryDepositLoanMapper;
import com.ttg.model.pojo.CostManageIndustryDepositLoan;
import com.ttg.model.vo.CMIndustryDepositVO;
import com.ttg.web.dao.CostManageIndustryDepositLoanDao;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/9/3 11:11
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Service
public class CostManageIndustryDepositLoanDaoImpl extends ServiceImpl<CostManageIndustryDepositLoanMapper, CostManageIndustryDepositLoan> implements CostManageIndustryDepositLoanDao {

    @Override
    public CostManageIndustryDepositLoan getByUnique(String intervalType,String year,String month, String clsId1, String clsId2) {
        LambdaQueryWrapper<CostManageIndustryDepositLoan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CostManageIndustryDepositLoan::getCmidIntervalType,intervalType)
                .eq(CostManageIndustryDepositLoan::getCmidYear,year)
                .eq(CostManageIndustryDepositLoan::getCmidMonth,month)
                .eq(CostManageIndustryDepositLoan::getCmidClsId1,clsId1)
                .eq(CostManageIndustryDepositLoan::getCmidClsId2,clsId2);
        return getOne(wrapper);
    }

    @Override
    public void saveOrUpd(CostManageIndustryDepositLoan industryDepositLoan) {
        CostManageIndustryDepositLoan old = getByUnique(industryDepositLoan.getCmidIntervalType(),industryDepositLoan.getCmidYear(),industryDepositLoan.getCmidMonth(),industryDepositLoan.getCmidClsId1(),industryDepositLoan.getCmidClsId2());
        if(Objects.isNull(old)){
            save(industryDepositLoan);
        }else{
            industryDepositLoan.setCmidId(old.getCmidId());
            updateById(industryDepositLoan);
        }
    }

    @Override
    public List<String> selectYears() {
        return baseMapper.selectYears();
    }

    @Override
    public List<String> selectMonths(String year) {
        return baseMapper.selectMonths(year);
    }

    @Override
    public BasePage<CMIndustryDepositVO> industryDeposit(BasePage page, CMReportDTO reqDto) {
        return baseMapper.industryDeposit(page,reqDto);
    }
}
